package com.ldl.exam.controller;

import com.ldl.exam.filter.StuLoginInterceptor;
import com.ldl.exam.filter.UserLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author liudaolin
 * @version 1.0
 * @date 2022/10/04 21:57:36
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    // 在此类中根据实现的方法,可以配置一些与SpringMVC相关的配置
    @Autowired
    private StuLoginInterceptor stuLoginInterceptor;
    @Autowired
    private UserLoginInterceptor userLoginInterceptor;

    // 静态资源访问
    // (默认不需要配置, 默认访问时, url中不需要static, 直接写 static后面的相对路径)
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置 url, 资源路径
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
                //.setCacheControl(CacheControl.noCache())
    }
    // 配置视图的直接访问
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 设置 url, viewName
        //registry.addViewController("/showLogin").setViewName("login");
        registry.addViewController("/").setViewName("redirect:/public/login.html");
    }
    // 配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addInterceptor 参数是 拦截器bean,
        // 返回当前拦截器配置对象, 用来配置拦截器具体配置
        registry.addInterceptor(stuLoginInterceptor)
                // 拦截谁,相当于<mvc:mapping path="/**"/>配置
                .addPathPatterns("/front/**")
                // 不拦截谁,相当于<mvc:exclude-mapping path="/test/add"/>配置
                // 在配置文件中可以配置多次拦截谁与不拦截谁
                // 此时我们可以将多个配置合并成一个方法
                // 方法的参数是一个可变长字符串或者字符串集合
                // 想要配置的请求有哪些就写几个
                .excludePathPatterns("/front/stu/login", "/front/stu/register", "/front/stu/logout", "/public/**")
                // 不拦截 以 /static 开头的(静态资源)
                .excludePathPatterns("/static/**", "/css/**", "/js/**", "/img/**", "/json/**");

        registry.addInterceptor(userLoginInterceptor)
                .addPathPatterns("/back/**", "/manage/**")
                .excludePathPatterns("/manage/login.html", "/back/user/login", "/back/user/logout", "/public/**")
                .excludePathPatterns("/static/**", "/css/**", "/js/**", "/img/**", "/json/**");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedHeaders("*")
                //.allowedOrigins("*")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .maxAge(3600);
    }


    // 自定义 错误状态 处理
    @Bean
    public MyErrorPageRegistrar errorPageRegistrar(){
        return new MyErrorPageRegistrar();
    }
    private class MyErrorPageRegistrar implements ErrorPageRegistrar {
        @Override
        public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
            ErrorPage page404 = new ErrorPage(HttpStatus.NOT_FOUND, "/public/404.html");
            ErrorPage page500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/public/error.html");
            errorPageRegistry.addErrorPages(page404, page500);
        }
    }
}
